/**
 * Created with  SublimeText
 * @Author:      Lyle
 * @DateTime:    2016-11-10 11:03:29
 * @Description: 
 */
define(function(require, exports, module) {

    var tthis = this,
        routes = {
            '/:module': function(module) { routerHandle(module, 'param', '') },
            '/:module/:type': function(module, type) { routerHandle(module, type, '') },
            '/:module/:type/(.*)': function(module, type, param) { routerHandle(module, type, param) },
            '/.*': function(module) { routerHandle('error', 'page', '') },
        };

    function routerHandle(module, type, param) {
        var obj = {
            name: module,
            type: type,
            param: param
        };
        dispatcher(obj);
    }

    function dispatcher(obj) {
        console.log('router', obj);
        require.async(getModulePath(obj.name), function(module) {
            if (module) {
                seajs.moduleName = obj.name;
                seajs.module = module;
                module.baseRender(obj);
                $('head').find('script').remove();
            } else {
                alert('Loading module failed. Please refresh and try again!');
            }
        });

    }

    function getModulePath(value) {

        var moduleName = value;

        if (typeof(Config.ABTest) !== 'undefined' && Config.ABTest[value]) {
            value = Config.ABTest[value];
        }

        var modulePath = '{base}/pages/' + value;

        if (typeof(Config.moduleDiff) !== 'undefined' && _.indexOf(Config.moduleDiff, moduleName) > -1) {
            modulePath = '{site}/pages/' + value;
        }
        //console.log(modulePath);
        return modulePath;
    }

    module.exports = routes;
});
